﻿/**
 ** @作者:	   王桂龙 QQ731513122
 ** @日期:	   2019.12.6
 ** @简介:     电量显示控件
 **            带有充电状态动画           
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BHXJDOTNET.NEW
{
    public partial class BHXJBattery : UserControl,IBHXJZOOM
    {
        #region 继承于ZOOM接口的属性和方法
        private Point _OriginPoint = new Point(0, 0);
        /// <summary>
        /// 原点
        /// </summary>
        public Point OriginPoint
        {
            get
            { return this._OriginPoint; }
            set
            {
                if (this._OriginPoint != value)
                {
                    this._OriginPoint = value;
                    if (DesignMode)
                        this.IniLocation = this.BHXJGetIniLocationInDesignMode();
                }
            }
        }
        private bool _AutoOrigin = false;
        /// <summary>
        /// 自动更改原点模式(只在设计模式下有用)
        /// </summary>
        public bool AutoOrigin
        {
            get
            {
                return this._AutoOrigin;
            }
            set
            {
                if (this._AutoOrigin != value)
                {
                    this._AutoOrigin = value;
                    if (DesignMode)
                    {
                        if (this.AutoOrigin)
                        {
                            this.OriginPoint = this.Location;
                        }
                        else
                        {
                            this.OriginPoint = new Point(0, 0);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 初始尺寸
        /// </summary>
        [Browsable(false)]
        public Size IniSize { get; set; }
        /// <summary>
        /// 初始位置
        /// </summary>
        [Browsable(false)]
        public Point IniLocation { get; set; }
        /// <summary>
        /// 初始字体
        /// </summary>
        [Browsable(false)]
        public Font IniFont { get; set; }

        #endregion


        private double _Power = 0.0;
        /// <summary>
        /// 电量
        /// </summary>
        [Description("电量"), Category("BHXJ")]
        public double Power
        {
            get { return this._Power; }
            set 
            {
                if (this._Power != value)
                {
                    this._Power = (value > 1) ? 1 : value;
                    this._Power = (this._Power < 0) ? 0 : this._Power;
                    //电量变化重绘
                    this.changedFlag=true;
                }
            }
        }

        /// <summary>
        /// _告警电量
        /// </summary>
        public double _WarningPower = 0.25;
        /// <summary>
        /// 告警电量
        /// </summary>
        [Description("告警电量"), Category("BHXJ")]
        public double WarningPower
        {
            get { return this._WarningPower; }
            set
            {
                if (this._WarningPower != value)
                {
                    this._WarningPower = value;
                    this.changedFlag = true;
                }
            }
        }


        /// <summary>
        /// _充电中
        /// </summary>
        private bool _Charging = false;
        /// <summary>
        /// 充电中
        /// </summary>
        [Description("充电中"), Category("BHXJ")]
        public bool Charging
        {
            get { return this._Charging; }
            set 
            {
                if (this._Charging != value)
                {
                    this._Charging = value;
                    this.chargingpower = -1;
                    this.changedFlag = true;
                }
            }
        }

        public BHXJBattery()
        {
            InitializeComponent();

            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);


            #region 设计时,变化事件
            if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
            {
                //设计时事件
                this.SizeChanged += new EventHandler(this.BHXJ_SizeChangedInDesignMode);
                this.FontChanged += new EventHandler(this.BHXJ_FontChangedInDesignMode);
                this.LocationChanged += new EventHandler(this.BHXJ_LocationChangedInDesignMode);
            }
            #endregion

            //防止字体在设计器中添加该控件时候自动追随父控件
            this.Font = this.Font;
            //缩放模式设置为无
            this.AutoScaleMode = AutoScaleMode.None;

            //设置初始
            this.IniSize = this.Size;
            this.IniFont = this.Font;
            this.IniLocation = this.BHXJGetIniLocationInDesignMode();

            this.getColorBlend();
            if (!this.DesignMode)
            {
                timerRefresh.Start();
            }

            
            ttp.InitialDelay = 200;
            ttp.AutoPopDelay = 10 * 1000;
            ttp.ReshowDelay = 200;
            ttp.ShowAlways = true;
            //ttp.IsBalloon = true;
            ttp.SetToolTip(this, "电量"); // ckbOverwrite is a checkbox  

            this.MouseEnter += BHXJBattery_MouseEnter;        
        }

        private void BHXJBattery_MouseEnter(object sender, EventArgs e)
        {

            int show = (int)(this._Power * 100);

            ttp.SetToolTip(this, "电量:" + show.ToString() + "％");
        }

        ToolTip ttp = new ToolTip();


        bool changedFlag = true;
        double chargingpower = 0;
        /// <summary>
        /// 边框
        /// </summary>
        ColorBlend colorBlendFrame=new ColorBlend(3);
        /// <summary>
        /// 电量正常
        /// </summary>
        ColorBlend colorBlendPower=new ColorBlend(3);
        /// <summary>
        /// 电量告警
        /// </summary>
        ColorBlend colorBlendWarningPower = new ColorBlend(3);

        /// <summary>
        /// 重载paint函数
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics gs = e.Graphics;
            LinearGradientBrush lgb = new LinearGradientBrush(new Point(0, 0), new Point(0, this.Height), Color.White, Color.Black);

            //边缘
            int edgw = (this.Width / 40 > 1) ? this.Width / 40 : 1;
            int edgh = (this.Height / 16 > 1) ? this.Height / 16 : 1;

            int a= edgw > edgh ? edgw = edgh : edgh = edgw;
          
            //区域
            GraphicsPath gpf = new GraphicsPath();
            gpf.AddLine(0, 0, this.Width - edgw, 0);
            gpf.AddLine(this.Width - edgw, 0, this.Width - edgw, this.Height / 4);
            gpf.AddLine(this.Width - edgw, this.Height / 4, this.Width, this.Height / 4);
            gpf.AddLine(this.Width, this.Height / 4, this.Width, this.Height*3 / 4);
            gpf.AddLine(this.Width, this.Height * 3 / 4, this.Width - edgw, this.Height * 3 / 4);
            gpf.AddLine(this.Width - edgw, this.Height * 3 / 4, this.Width - edgw, this.Height);
            gpf.AddLine(this.Width - edgw, this.Height, 0, this.Height);
            Region regf = new Region(gpf);
            this.Region = regf;
            
            //中间显示电量区域不绘制背景       
            regf.Xor(new RectangleF(edgw, edgh, (float)((this.Width - 3 * edgw)), this.Height - 2 * edgh));
            gs.SetClip(regf,CombineMode.Intersect);
      
            lgb.InterpolationColors = this.colorBlendFrame;
            gs.FillRectangle(lgb, this.ClientRectangle);

            gs.ResetClip();

            double power = this._Power;
            if (this._Power < 1)
            {
                if (this.Charging)
                {
                    power += this.chargingpower;
                }
                if (power > 1)
                {
                    if (this.chargingpower > (1 - this.Power) * 1.1)
                        this.chargingpower = -1;
                    power = 1;
                }
                if (power < 0)
                {
                    power = 0;
                }
            }
            if (this._Power > this._WarningPower)
            {
                lgb.InterpolationColors = this.colorBlendPower;
            }
            else
            {
                lgb.InterpolationColors = this.colorBlendWarningPower;
            }
            gs.FillRectangle(lgb, edgw, edgh, (float)((this.Width - 3 * edgw) * power), this.Height - 2 * edgh);
            lgb.Dispose();
        }
        /// <summary>
        /// 获取ColorBlend
        /// </summary>
        private void getColorBlend()
        {
            Color[] framecolors = 
            {  Color.FromArgb(64,64,64),  
                Color.FromArgb(200,200,200), 
                Color.FromArgb(64,64,64)   
            };
            Color[] powercolors = 
            {  
                Color.DarkGreen,  
                Color.Lime,  
                Color.DarkGreen      
            };

            Color[] warningpowercolors = 
            {  
                Color.DarkRed,  
                Color.Red,  
                Color.DarkRed      
            };

            float[] positions = { 0.0f, 0.6f, 1.0f };
            this.colorBlendFrame = new ColorBlend(3);
            this.colorBlendFrame.Colors = framecolors;
            this.colorBlendFrame.Positions = positions;

            this.colorBlendPower = new ColorBlend(3);
            this.colorBlendPower.Colors = powercolors;
            this.colorBlendPower.Positions = positions;

            this.colorBlendWarningPower = new ColorBlend(3);
            this.colorBlendWarningPower.Colors = warningpowercolors;
            this.colorBlendWarningPower.Positions = positions;
        }

        /// <summary>
        /// 获取充电时每周期电量增加量
        /// </summary>
        /// <returns></returns>
        private double getChargEnergy()
        {
            return (0.017 * (1 - this._Power));
        }


        private void timerRefresh_Tick(object sender, EventArgs e)
        {
            if (this.changedFlag)
            {
                this.changedFlag = false;
                //if (this.Charging && (!this.DesignMode) && (this._Power < 1))
                if (this.Charging && (this._Power < 1))
                {
                    if (this.chargingpower < 0)
                        this.chargingpower = 0;
                    else
                        this.chargingpower += getChargEnergy();
                    this.changedFlag = true;
                }                    
                this.Invalidate();
            }
        }
    }
}
